*DATA PROCESSING

*get original wide form data
clear
use "[filepath redacted for review]/CCES18_[module name redacted for review].DTA"

*text references: n's by sex
tab gender_post

*note: hereafter "case study" refers to the three subjects of the vignettes (i.e., assage, manning, snowden)

*text reference: n of participants by case study and treatment condition
	*assange
tab MIA411_treat
	*manning
tab MIA410_treat
	*snowden
tab MIA412_treat

*text reference for how yougov/CCES pid 3 was coded (note that they exclude leaners from dem/rep)
tab pid7 pid3
	*recode pid3 into rep = 0 , dem = 1, all others = 3 (leaners, indys, and dk)
generate dem=2
replace dem=1 if pid3==1
replace dem=0 if pid3==2
label define dem_label 0 "REP" 1 "DEM" 2 "all others"
label values dem dem_label
tab pid3 dem

*clean up names of the DVs for conversion to long form
generate feel1=MIA411_1
generate feel2=MIA410_1
generate feel3=MIA412_1
pwcorr feel1 MIA411_1
pwcorr feel2 MIA410_1
pwcorr feel3 MIA412_1

*keep what we need
keep caseid dem MIA411_1 MIA410_1 MIA412_1 MIA411_treat MIA410_treat MIA412_treat feel1 feel2 feel3

*generate long form data set
reshape long feel, i(caseid) j(casestudy)
	*label new var "casestudy"
label define case_label 1 "assange" 2 "manning" 3 "snowden"
label values casestudy case_label
tab casestudy

*generate summary treat indicator (combine the three case studies)
generate treat=.
replace treat=1 if MIA410_treat==1 & casestudy==2
replace treat=2 if MIA410_treat==2 & casestudy==2
replace treat=3 if MIA410_treat==3 & casestudy==2
replace treat=4 if MIA410_treat==4 & casestudy==2
replace treat=1 if MIA411_treat==1 & casestudy==1
replace treat=2 if MIA411_treat==2 & casestudy==1
replace treat=3 if MIA411_treat==3 & casestudy==1
replace treat=4 if MIA411_treat==4 & casestudy==1
replace treat=1 if MIA412_treat==1 & casestudy==3
replace treat=2 if MIA412_treat==2 & casestudy==3
replace treat=3 if MIA412_treat==3 & casestudy==3
replace treat=4 if MIA412_treat==4 & casestudy==3
tab treat

*save out long form data for analysis and peer-reivew replication
save "[filepath redacted for review]/wb or leak long form data.dta"

*ANALYSIS

*get long form data
clear
use "[filepath redacted for review]/wb or leak long form data.dta"

*reset matrix size to accomodate the repeated measures ANOVAs
set matsize 10000

*increase memory for rep meas ANOVAs
set memory 20000

*effects by case study
	*repeated measures anova for within-subjects effects by case study
anova feel caseid casestudy, repeated(casestudy)
margins casestudy

*effects by vignette
	*anova for between-subjects effects by vignette
anova feel caseid treat
margins treat

*combined effects of case study and treatment
	*mixed-model anova with repeeated measures effects for case study and between-subjects effects for vignette
anova feel treat / caseid|treat casestudy casestudy#treat, repeated(casestudy)
margins casestudy

*combined effects of case study and treatment by PID
	*dems
		*restrict sample to democrats
keep if dem==1
tab dem
		*rep measures anova
anova feel treat / caseid|treat casestudy casestudy#treat, repeated(casestudy)
			*main effect of casestudy
pwcompare casestudy, asbalanced emptycells(reweight)
			*main effect of vignette
margins treat, asbalanced emptycells(reweight)
pwcompare treat, asbalanced emptycells(reweight)
			*interaction effect of case study and vignette
margins casestudy#treat, asbalanced emptycells(reweight)
	*reps
		*restrict sample to democrats (be sure to get the full long-form data set first since you filtered them out for the DEM run above)
keep if dem==0
tab dem
		*rep measures anova
anova feel treat / caseid|treat casestudy casestudy#treat, repeated(casestudy)
			*main effect by casestudy
margins casestudy, asbalanced emptycells(reweight)
pwcompare casestudy, asbalanced emptycells(reweight)
			*main effect by vignette
margins treat, asbalanced emptycells(reweight)
pwcompare treat, asbalanced emptycells(reweight)
			*interaction effect of case study and vignette
margins casestudy#treat, asbalanced emptycells(reweight)
pwcompare casestudy#treat, asbalanced emptycells(reweight)
